package development.app;

import state.GameForm;
import state.Graphics;
import state.Key;
import state.component.Event;
import state.component.EventListener;
import state.component.GridMenu;
import state.socket.DataPackage;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.TwoDimensionString;
import state.util.array.IntArray;
import state.util.array.ObjectArray;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.Global;
import development.app.mxh.FrmAccount;
import development.app.mxh.FrmMessage;
import development.app.mxh.FrmMyAlbum;
import development.app.mxh.FrmUserGroup;
import development.app.mxh.FrmWall;
import development.group.FrmGroupFriendAndTop;
import development.group.FrmGroupKetBanMangXHChat;
import development.group.FrmGroupMenu;
import development.socket.ProtocolConstants;

public class FrmMangXH extends GameForm implements EventListener {

  private StringArray MANGXH_NAME;
  private StringArray MANGXH_CODE;
  private TwoDimensionString MANGXH_TOOLTIP;

  private static GameForm singleton;
  private ObjectArray itemsImages;

  private int HOZ_DISTANCE;
  private int VER_DISTANCE;
  private int DX;
  private int DY;
  private int NUMBER_OF_ITEM_PER_ROW;
  private int numberOfItem;
  StringArray listTypeDraw;
  String type;
  String categorycode;
  String categoryname;
  int MAXPAGE = 1;
  int totalcount;
  int MAXROW = 10;
  int countpercent;
  int distanceY = 0;
  public IntArray eventNumber;

  private Hashtable parameters;

  private GridMenu gridMenu;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmMangXH();
    }
    return singleton;
  }

  public FrmMangXH() {
    MANGXH_NAME = new StringArray(new String[] { "Cá nhân", "Bạn bè", "Bang hội", "Hòm thư", "Tường", "Bảng vàng", "Hot Album", "Ảnh của tôi" }, 8);
    MANGXH_CODE = new StringArray(new String[] { "CANHAN", "BANBE", "BANGHOI", "HOMTHU", "CAOTHU", "DAIGIA", "ALBUM", "MYIMAGE" }, 8);
    listTypeDraw = new StringArray(new String[] { "drawMain", "drawList", "drawCreate", "drawEdit", "drawDetail" }, 5);
    
    MANGXH_TOOLTIP = new TwoDimensionString(1, 8);
    MANGXH_TOOLTIP.setColumn(0, new StringArray(new String[]  { "Cá nhân" ,  "Bạn bè" ,  "Bang hội" ,  "Hòm thư" ,  "Tường" , "Bảng vàng" ,  "Hot Album" ,  "Ảnh Của Tôi"  }, 8));
  }

  public void init(Hashtable hashtable) {
    this.parameters = hashtable;
    categorycode = MANGXH_CODE.get(0);
    numberOfItem = MANGXH_CODE.len();
    itemsImages = ObjectArray.create(numberOfItem);
    itemsImages.set(0, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "1.Ca-nhan.png"));
    itemsImages.set(1, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "2.Ban-be.png"));
    itemsImages.set(2, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "3.Bang-hoi.png"));
    itemsImages.set(3, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "4.Hom-thu.png"));
    itemsImages.set(4, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "tuong_icon.png"));
    itemsImages.set(5, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "6.Dai-gia.png"));
    itemsImages.set(6, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "7.Hot-album.png"));
    itemsImages.set(7, ImageUtil.getImage(Constants.MANGXH_CATEGORY, "8.Anh.png"));
    eventNumber = IntArray.create(numberOfItem);
    type = listTypeDraw.get(0);
    initDraw();
    gridMenu = new GridMenu(_manager, itemsImages, MANGXH_NAME, NUMBER_OF_ITEM_PER_ROW, DX, DY, HOZ_DISTANCE, VER_DISTANCE, this);
    gridMenu.setToolTip(MANGXH_TOOLTIP);
    gridMenu.setForceGroundColor(GameColor.WHITE_CODE);
    gridMenu.setFocusFgColor(GameColor.YELLOW_CODE);
    gridMenu.setEventNumbers(eventNumber);
    // AccountInfo
    countpercent = 100;

    Global.authenClient.write(DataPackage.createDataPackage1(ProtocolConstants.RequestHeader.MESSAGE_UNREAD_COUNT_REQUEST));
  }

  public GridMenu getGridMenu() {
    return gridMenu;
  }

  private void initDraw() {
    if (Constants.IS_240x320_SCREEN) {
      if (type.equalsIgnoreCase(listTypeDraw.get(0))) {
        HOZ_DISTANCE = 70;
        VER_DISTANCE = 60;
        DX = 50;
        DY = 60;
        NUMBER_OF_ITEM_PER_ROW = 3;
      } else {
        NUMBER_OF_ITEM_PER_ROW = 1;
      }
    } else {
      if (type.equalsIgnoreCase(listTypeDraw.get(0))) {
        HOZ_DISTANCE = 70;
        VER_DISTANCE = 60;
        DX = 50;
        DY = 60;
        NUMBER_OF_ITEM_PER_ROW = 4;
      } else {
        NUMBER_OF_ITEM_PER_ROW = 1;
      }
    }
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    drawMain(g);
  }

  private void drawMain(Graphics g) {
    DrawUtils.setBottomBar(_manager, "", "Chọn", "Trở về");
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.ENTER:
      chooseMenu(gridMenu.getCurrentSelectedIndex());
      break;
    case Key.SOFT_RIGHT:
      if (parentGroup != null) {
        parentGroup.goBack();
      } else {
        Global.nextState(FrmGroupMenu.instance(), null);
      }
      break;
    }
  }

  private void chooseMenu(int choosedIndex) {
    switch (choosedIndex) {
    case 0: // Ca-nhan
      FrmAccount.backForm = FrmGroupKetBanMangXHChat.instance();
      if (parameters == null) {
        parameters = new Hashtable();
      }
      parameters.put("User", Global.currentUser.getName());
      Global.nextState(FrmAccount.instance(), parameters);
      break;
    case 1: // Ban-be
      Global.nextState(FrmGroupFriendAndTop.instance(), null);
      FrmGroupFriendAndTop.backForm = FrmGroupKetBanMangXHChat.instance();
      break;
    case 2:// bang hoi
      Global.nextState(FrmUserGroup.instance(), null);
      break;
    case 3:
      Global.nextState(FrmMessage.instance(), null);
      break;
    case 4: // Tuong
      parameters = new Hashtable();
      parameters.put("wallType", String.valueOf(FrmWall.MY_WALL_TYPE));
      FrmWall.backForm = parentGroup;
      Global.nextState(FrmWall.instance(), parameters);
      break;
    case 5: // Dai-gia
      Global.nextState(FrmGroupFriendAndTop.instance(), null);
      FrmGroupFriendAndTop.backForm = FrmGroupKetBanMangXHChat.instance();
      FrmGroupFriendAndTop.setTab(1);
      break;
    case 6: // Album hot
      parameters = new Hashtable();
      parameters.put("albumType", String.valueOf(FrmMyAlbum.HOT_ALBUM_TYPE));
      parameters.put("User", Global.currentUser.getName());
      FrmMyAlbum.backForm = parentGroup;
      Global.nextState(FrmMyAlbum.instance(), parameters);
      break;
    case 7: // My album
      parameters = new Hashtable();
      parameters.put("albumType", String.valueOf(FrmMyAlbum.MY_ALBUM_TYPE));
      parameters.put("User", Global.currentUser.getName());
      FrmMyAlbum.backForm = parentGroup;
      Global.nextState(FrmMyAlbum.instance(), parameters);
      break;
    }
  }

  public void onRecieveData(DataPackage dataPackage) {
  }

  public void onActionPerform(Event event) {
    if (event.getSource() == gridMenu) {
      chooseMenu(gridMenu.getCurrentSelectedIndex());
    }
  }

  public void setUnreadMessage(int count) {
    eventNumber.set(3, count);
  }

  public void destroy() {
    itemsImages = null;
    type = null;
    categorycode = null;
    categoryname = null;
    parameters = null;
    gridMenu = null;
  }
}
